package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.CountrySpecification;
import dbHelper.DBHelper;
import entities.Country;

public class CountryRepository implements Repository<Country> {
	
	public ArrayList<Country> CountryList;
	
	
	public CountryRepository()
	{
		this.CountryList = null;
	}

	@Override
	public void Add(Country Country) {
		try {
			DBHelper.addObject(Country);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Country Country) {
		try {
			DBHelper.deleteObject(Country);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Country Country) {
		try {
			DBHelper.updateObject(Country);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Country C = new Country();
		try {
			this.CountryList = (ArrayList<Country>) DBHelper.findObjects(C);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Country> findBySpecification (CountrySpecification spec) {
		if(this.CountryList == null)
			this.FindAll();
		ArrayList<Country> res = new ArrayList<Country>();
		for (Country el : CountryList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}

}
